package com.example.server.controller;

import com.example.server.entity.Collect;
import com.example.server.redis.RedisService;
import com.example.server.service.CollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("collect")
public class CollectController extends BaseController{

    private final CollectService collectService;
    @Autowired
    private RedisService redisService;

    public CollectController(CollectService collectService) {
        this.collectService = collectService;
    }

    @PostMapping("create")
    private int create(){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        int pid = Integer.parseInt(redisService.getSetString("productId"));
        Collect collect = new Collect();
        collect.setUid(uid);
        collect.setPid(pid);

        return collectService.create(collect);
    }

    @GetMapping("queryByUid")
    private List<Collect> queryByUid(){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        return collectService.queryList("queryByUid",uid);
    }
    @GetMapping("querySelect")
    private Collect querySelect(){
        Collect collect = new Collect();
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        int pid = Integer.parseInt(redisService.getSetString("productId"));

        collect.setUid(uid);
        collect.setPid(pid);

        return collectService.queryOne("querySelect",collect);
    }

    @DeleteMapping("removeCollect")
    private int remove(){
        Collect collect = new Collect();
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        int pid = Integer.parseInt(redisService.getSetString("productId"));

        collect.setUid(uid);
        collect.setPid(pid);

        return collectService.removeOne("removeCollect",collect);
    }


    @DeleteMapping("delete")
    private int removeById(@RequestBody Map<String,Integer> map){
        int id = map.get("id");
        return collectService.removeById(id);
    }

    @DeleteMapping("delete/{id}")
    private int removeById(@PathVariable int id){
        return collectService.removeById(id);
    }
}
